package com.xywy.common.widget;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xywy.R;


/**
 * 自定义标题栏
 *
 * @author xgd
 */
public class TitleBar extends RelativeLayout {
    private  RelativeLayout  rlTitleBar;
    private Button back;
    private TextView tvBack;
    private TextView go;
    private TextView title;

    public TitleBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        inflateView(context);
        TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.title_view);

        if (!a.getBoolean(R.styleable.title_view_go_isvisible, true))
            go.setVisibility(INVISIBLE);
        if (!a.getBoolean(R.styleable.title_view_back_isvisible, true))
            back.setVisibility(INVISIBLE);

        String s = a.getString(R.styleable.title_view_title1);
        if (null != s) {
            title.setText(s);
        }
        a.recycle();
    }

    public TitleBar(Context context) {
        super(context);
        inflateView(context);
    }

    /**
     * 设置标题栏的标题图片(标题图片默认不显示)
     * @param res
     */
    public void setTitleImg(int res){
        title.setText("");
    }

    public TitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        inflateView(context);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.title_view);
        if (isInEditMode()) {
            return;
        }
        if (!a.getBoolean(R.styleable.title_view_go_isvisible, true))
            go.setVisibility(View.INVISIBLE);
        if (!a.getBoolean(R.styleable.title_view_back_isvisible, true))
            back.setVisibility(View.INVISIBLE);

        String s = a.getString(R.styleable.title_view_title1);
        if (null != s) {
            title.setText(s);
        }
        Drawable back_src = a.getDrawable(R.styleable.title_view_back_src);
        Drawable go_src = a.getDrawable(R.styleable.title_view_go_src);
        if (null != back_src)
            back.setBackgroundDrawable(back_src);
        if (null != go_src)
            go.setBackgroundDrawable(go_src);

        a.recycle();
    }

    private void inflateView(Context context) {
        //此处的viewgroup若不设置,将无法显示
        View view = View.inflate(context, R.layout.title_bar, this);
        rlTitleBar = (RelativeLayout) view.findViewById(R.id.rl_title_bar);
        back = (Button) view.findViewById(R.id.bt_title_back);
        tvBack = (TextView) view.findViewById(R.id.tv_title_back);
        go = (TextView) view.findViewById(R.id.tv_title_go);
        title = (TextView) view.findViewById(R.id.tv_title);
        if (isInEditMode()){
            return;
        }

        tvBack.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        back.getBackground().setAlpha(180);
                        break;
                    case MotionEvent.ACTION_UP:
                        back.getBackground().setAlpha(255);
                        break;
                }
                return false;
            }
        });

        back.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        back.getBackground().setAlpha(180);
                        break;
                    case MotionEvent.ACTION_UP:
                        back.getBackground().setAlpha(255);
                        break;
                }
                return false;
            }
        });

        go.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        if(null!=go.getBackground()){
                            go.getBackground().setAlpha(180);
                        }
                        break;
                    case MotionEvent.ACTION_UP:
                        if(null!=go.getBackground()){
                            go.getBackground().setAlpha(255);
                        }
                        break;
                }
                return false;
            }
        });

        setOnClickListener( new OnClickListener() {
            @Override
            public void onClick(View view) {
//                用于解决，会点击到下层的问题
            }
        });

    }

    /**
     * 获得左侧返回按钮
     * id:title_back
     * @return 返回button
     */
    public Button getBack() {
        return back;
    }

    /**
     * 返回事件
     * @param activity
     */
    public  void  onClickBack(final Activity activity){
        back.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                activity.finish();
            }
        });
        tvBack.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                activity.finish();
            }
        });
    }

    /**
     * 获得左侧返回按钮
     * id:title_back
     * @return 返回button
     */
    public TextView getTvBack() {
        return tvBack;
    }

    /**
     * 获得右侧前进按钮
     * id:title_go
     * @return 前进button
     */
    public TextView getGo() {
        return go;
    }

    /**
     * 设置返回按钮是否可见
     *
     * @param b true为可见 否则不可见
     */
    public void backIsVisible(boolean b) {
        if (b)
            back.setVisibility(View.VISIBLE);
        else
            back.setVisibility(View.INVISIBLE);
    }

    /**
     * 设置前进按钮是否可见
     *
     * @param b true为可见 否则不可见
     */
    public void goIsVisible(boolean b) {
        if (b)
            go.setVisibility(View.VISIBLE);
        else
            go.setVisibility(View.INVISIBLE);
    }

    /**
     * 设置标题栏文字
     *
     * @param title
     */
    public void setTitle(String title) {
        if (null == title) {
            this.title.setText("");
        } else {
            this.title.setText(title);
        }
    }

    public TextView getTitle() {
        return title;
    }

    /**
     * 设置titleBar背景颜色
     * @param color
     */
    public void  setTitleBarBackGroud(int color){
         rlTitleBar.setBackgroundColor(getResources().getColor(color));
    }
}
